// lib/views/group_list_screen.dart
import 'package:chat/controller/group_list_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

final GroupListController controller = Get.find();
final TextEditingController searchController = TextEditingController();

class GroupListScreen extends StatelessWidget {
  const GroupListScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: const Text('我的群组'),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0,
        foregroundColor: Colors.black,
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: TextField(
              controller: searchController,
              decoration: InputDecoration(
                hintText: '搜索好友',
                prefixIcon: const Icon(Icons.search),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(10),
                  borderSide: BorderSide.none,
                ),
                filled: true,
                fillColor: Colors.grey[200],
                contentPadding: const EdgeInsets.symmetric(vertical: 0),
              ),
              onChanged: (value) {
                controller.searchGroups(value);
              },
            ),
          ),
          Expanded(
            child: Obx(
              () => ListView.builder(
                itemCount: controller.groups.length,
                itemBuilder: (context, index) {
                  final group = controller.groups[index];
                  return Container(
                    padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16), // 匹配 ListTile 默认间距
                    child: Row(
                      children: [
                        // 圆形头像（完全复用 CircleAvatar）
                        InkWell(
                          onTap: () {
                            Get.toNamed('/groupDetailScreen'); // 保持点击跳转逻辑
                          },
                          child: CircleAvatar(
                            backgroundImage: AssetImage(group.avatarUrl), // 网络图片
                            radius: 20, // 默认大小（与 ListTile 的 CircleAvatar 一致）
                          ),
                        ),
                        SizedBox(width: 16), // 模拟 ListTile 的 leading 和 title 间距
                        // 标题部分
                        Expanded(
                          child: InkWell(
                            onTap: () {
                              Get.toNamed('/groupChatScreen'); // 保持点击跳转逻辑
                            },
                            child: Text(
                              group.name,
                              style: TextStyle(fontSize: 16), // 可选：自定义字体样式
                            ),
                          ),
                        ),
                      ],
                    ),
                  );
                },
              ),
            ),
          ),
        ],
      ),
    );
  }
}
